﻿using Iworld.Author.Model;
using Iworld.Basis.Help;
using Iworld.Basis.Manager;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Iworld.Author.Manager
{
    /// <summary>
    ///充值卡管理者对象
    /// </summary>
    [RegisterToManagerService]
    public class RechargeCardManager : ManagerBase<IModelToDbContextOfAuthor, RechargeCardManager.Actions, RechargeCard>
    {


        public RechargeCardManager(IModelToDbContextOfAuthor db)
            : base(db)
        {

        }

        /// <summary>
        /// 充值
        /// </summary>
        /// <param name="package"></param>
        public void Recharge(IPackageForRecharge package)
        {
            RechargeCard card = db.RechargeCards.FirstOrDefault(x => x.Account == package.Account && x.Password == package.Password);
            if(card == null)
            {
                throw new Exception("充值卡账号或密码错误");
            }
            if(card.IsUsed)
            {
                throw new Exception("充值卡已经被使用过");
            }
            Iworld.Author.Model.Author user = db.Authors.FirstOrDefault(x => x.Id == package.UserId);
            if(user == null)
            {
                throw new Exception("用户不存在!");
            }
            OnExecuting(Actions.Recharge, card);
            card.UsedTime = DateTime.Now;
            card.IsUsed = true;
            card.UsedAuthor = user;
            OnExecuted(Actions.Recharge, card);
            db.SaveChanges();
        }

        /// <summary>
        /// 移除用户的时候清除相关信息
        /// </summary>
        /// <param name="info"></param>
        [Listen(typeof(AuthorManager), AuthorManager.Actions.Remove, ExecutionOrder.Before)]
        public static void Monitor_RemoveRecordsWhenUserRemoving(InfoOfSendOnManagerService info)
        {
            IModelToDbContextOfAuthor db = (IModelToDbContextOfAuthor)info.Db;
            Model.Author model = (Model.Author)info.Model;
            db.RechargeCards.Where(x => x.UsedAuthor.Id == model.Id).ToList()
                .ForEach(x =>
                {
                    x.UsedAuthor = null;
                });
        }

        /// <summary>
        /// 生成充值卡
        /// </summary>
        /// <param name="package"></param>
        public RechargeCard Generate(IPackageForGenerate package)
        {
            if (package.Golds<=0)
            {
                throw new Exception("充值面额不能小于等于0");
            }
            RechargeCard rc = new RechargeCard();
            rc.Password = Guid.NewGuid().ToString();
            rc.Account = Guid.NewGuid().ToString();
            rc.Golds = package.Golds;
            RechargeCard check = db.RechargeCards.FirstOrDefault(x => x.Account == rc.Account);
            if (check != null)
            {
                throw new Exception("充值卡[" + rc.Account + "]已经存在");
            }
            rc.AdministratorId = package.AdministratorId;
            OnExecuting(Actions.Generate, rc);
            db.RechargeCards.Add(rc);
            OnExecuted(Actions.Generate, rc);
            db.SaveChanges();
            return rc;
        }



 
        #region 枚举

        /// <summary>
        /// 动作
        /// </summary>
        public enum Actions
        {
            /// <summary>
            /// 创建充值卡
            /// </summary>
            Create,
            /// <summary>
            /// 修改充值卡
            /// </summary>
            Update,
            /// <summary>
            /// 移除充值卡
            /// </summary>
            Remove,
            /// <summary>
            /// 充值
            /// </summary>
            Recharge,
            /// <summary>
            /// 生成充值卡
            /// </summary>
            Generate
        }
 

        #endregion

        #region 接口
        /// <summary>
        /// 充值数据集
        /// </summary>
       public interface IPackageForRecharge
       {
           /// <summary>
           /// 用户指针
           /// </summary>
           int UserId { set; get; }

           /// <summary>
           /// 充值卡账号
           /// </summary>
           string Account { get; set; }

           /// <summary>
           /// 充值卡密码
           /// </summary>
           string Password { set; get; }
       }

        /// <summary>
        /// 生成充值卡数据集
        /// </summary>
        public interface IPackageForGenerate
        {
            /// <summary>
            /// 面值
            /// </summary>
            double Golds { get; }

            /// <summary>
            /// 生成人的ID
            /// </summary>
            int AdministratorId { get; }
        }

        #endregion
       

    }
}
